<?php
namespace app\applet\model;

use app\manage\model\AdminUser;
use think\Db;
use think\Model;

class GasMeter extends Model {
    protected $table = "gas_meter";
    public function __construct($data = [])
    {
//        parent::__construct($data);
    }

    /**
     * @param $data
     * @return int|string
     */
    public function m_insert($data) {
        $data = Db::name($this->table)
            ->insert($data);
        return Db::name($this->table)->getLastInsID();
    }

    /**
     * 编辑
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function m_update($id, $data) {
        $data = Db::name($this->table)
            ->where(['id'=>$id])
            ->update($data);
        return $data;
    }

    /**
     * 获得当前电表热数据
     * @param $admin_id
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getNowList($admin_id, $tung, $unit, $layer, $status) {
        $where = [];
        if (isset($admin_id) && $admin_id !== '') {
            $where['admin_id'] = $admin_id;
        }
        if ($tung !== null) {
            $where['tung_num'] = $tung;
        }
        if ($tung == "") {
            unset($where['tung_num']);
        }
        if ($unit !== null) {
            $where['unit_num'] = $unit;
        }
        if ($unit == "") {
            unset($where['unit_num']);
        }
        if ($layer !== null) {
            $where['layer_num'] = $layer;
        }
        if ($layer == "") {
            unset($where['layer_num']);
        }
        if ($status !== null) {
            $where['status'] = $status;
        }
        if ($status == "") {
            unset($where['status']);
        }

        $where['is_newest'] = 1;
        $data['data'] = Db::name($this->table)
            ->field('id, house_id, type, tung_num, unit_num, layer_num, room_number, status')
            ->where($where)
            ->order('tung_num asc, unit_num asc, room_number asc')
            ->whereTime('create_time','month')
            ->select();
        $data['count'] = Db::name($this->table)
            ->where($where)
            ->whereTime('create_time','month')
            ->count();

        return $data;
    }

    /**
     * 获取全部列表
     * @param $admin_id
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList($admin_id, $status, $page, $keyword) {
        $where = [];
        if ($admin_id != 1) {
            $model = new AdminUser();
            $detail = $model->detail($admin_id);
            if ($detail['admin_sign'] != 1) {
                if ($admin_id !== null && $admin_id !== "") {
                    $where['admin_id'] = $admin_id;
                }
            }

        }
        if ($status !== null && $status !== "") {
            $where['pay'] = $status;
        }

        $where['is_newest'] = 1;
        $data['data'] = Db::name($this->table)
            ->field('id, house_id, type, tung_num, unit_num, layer_num, room_number, pay, pay_sum, pay_time, tenant_name, tenant_tel')
            ->where($where)
            ->where('tenant_name|tenant_tel', 'like', '%' . $keyword . '%')
            ->page($page, 10)
            ->order('tung_num asc, unit_num asc, layer_num asc, room_number asc')
            ->select();
        $data['count'] = Db::name($this->table)
            ->where($where)
            ->where('tenant_name|tenant_tel', 'like', '%' . $keyword . '%')
            ->count();

        return $data;
    }

    /**
     * 获取某个房间的电费列表
     * @param $id
     * @param $status
     * @param $pay
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getRoomList($id, $page) {
        $detail = Db::name($this->table)
            ->alias('e')
            ->field('e.tenant_name, e.id, e.meter_id, a.name, concat(e.tung_num, "-", e.unit_num, "-", e.room_number) as room_number, e.tung_num, e.unit_num, e.room_number')
            ->where('id', $id)
            ->join('admin_user a', 'a.admin_id=e.meter_id', 'LEFT')
            ->find();
        $data['room_number'] = $detail['room_number'];
        $data['tenant_name'] = $detail['tenant_name'];
//        $data['meter_id'] = $detail['meter_id'];

        $list = Db::name($this->table)
            ->alias('e')
            ->field('e.time, e.num, e.image, e.remark, a.name')
            ->join('admin_user a', 'a.admin_id=e.meter_id', 'LEFT')
//            ->where('e.meter_id', $data['meter_id'])
            ->where('e.tung_num', $detail['tung_num'])
            ->where('e.unit_num', $detail['unit_num'])
            ->where('e.room_number', $detail['room_number'])
            ->where('e.status', '<>', 1)
            ->page($page, get_list_rows())
            ->select();
        $data['data'] = $list;
        return $data;
//        $start_time = strtotime("-" . $page * get_list_rows() . "month");
//        $end_time = strtotime("-" . ($page-1) * get_list_rows() . "month");
//        if (isset($status) && $status !== "") {
//            $where['status'] = $status;
//        }
//        $detail = $this->detail($id);
//        $where['tung_num'] = $detail['tung_num'];
//        $where['unit_num'] = $detail['unit_num'];
//        $where['layer_num'] = $detail['layer_num'];
//        $where['room_number'] = $detail['room_number'];
//        $data['data'] = Db::name($this->table)
//            ->alias('g')
//            ->field('g.id, g.admin_id, g.house_id, g.type, g.tung_num, g.unit_num, g.layer_num, g.room_number, g.status, g.num, g.time, a.name')
//            ->where($where)
//            ->join('admin_user a', 'a.admin_id=g.meter_id')
//            ->whereTime('g.create_time','between', [$start_time, $end_time])
//            ->select();
//
//        $time_count = Db::name($this->table)
//            ->field('max(create_time) as max, min(create_time) as min')
//            ->where($where)
//            ->find();
//        $data['total'] = get_month_diff($time_count['min'], $time_count['max']) + 1;
//
//        $list = [];//处理数据，保证每月均有数据，并将同一月内的数据进行合并
//        for ($i = 0; $i < (($data['total']  > $page * get_list_rows()) ? get_list_rows() : ($data['total']  - ($page-1) * get_list_rows())); $i++) {
//            $list[$i]['month_time'] = date('Y-m', strtotime('-' . ($page-1)*get_list_rows()-$i . "month"));
//            foreach ($data['data'] as $value) {
//                if (get_month_time($list[$i]['month_time'], $value['time'])) {
//                    $list[$i]['time'][] = $value['time'];
//                    $list[$i]['num'][] = $value['num'];
//                    $list[$i]['name'][] = $value['name'];
//                }
//                $list[$i]['id'] = $value['id'];
//            }
//        }
//        foreach ($list as &$a) {
//            if (!isset($a['time'])) $a['time']=['--'];
//            if (!isset($a['num'])) $a['num']=['--'];
//            if (!isset($a['name'])) $a['name']=['--'];
//        }
//        $data['data'] = $list;
//
//        return $data;
    }

    /**
     * 获取一条记录详情
     * @param $id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getDetail($id) {
        $where['id'] = $id;
        $data = Db::name($this->table)
            ->field('id, admin_id, house_id, type, tung_num, unit_num, layer_num, room_number, status, num, time, image, remark')
            ->where($where)
            ->find();

        return $data;
    }

    /**
     * 获得待办事项数量
     * @return int|string
     * @throws \think\Exception
     */
    public function getTodo($admin_id) {
        $where['status'] = 1;
        $where['admin_id'] = $admin_id;
        $count = Db::name($this->table)
            ->where($where)
            ->whereTime('create_time','month')
            ->count();
        return $count;
    }

    /**
     * @param $admin_id
     * @param $page
     * @param $row
     * @param $keyword
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getIndex($admin_id, $page, $row, $status, $tung, $unit, $room_number, $time) {
        $where['e.admin_id'] = $admin_id;
        $where['is_newest'] = 1;
        if (isset($status) && $status !== "") {
            $where['e.status'] = $status;
        }
        if (isset($tung) && $tung !== "") {
            $where['e.tung_num'] = $tung;
        }
        if (isset($unit) && $unit !== "") {
            $where['e.unit_num'] = $unit;
        }
        if (isset($room_number) && $room_number !== "") {
            $where['e.room_number'] = $room_number;
        }
        if ($time == 1) {
            $time = 'month';
        } else {
            $time = 'last month';
        }
        $list = Db::name($this->table)
            ->alias('e')
            ->page($page, $row)
            ->field("e.id, a.name, e.num, concat(e.tung_num,'-',e.unit_num,'-',e.room_number) as root_number, 
            e.status, e.time, e.image, e.remark")
            ->join('admin_user a', 'e.meter_id=a.admin_id', 'LEFT')
            ->where($where)
            ->whereTime('e.create_time',$time)
            ->order('tung_num asc, unit_num asc room_number asc')
            ->select();
        $count = Db::name($this->table)
            ->alias('e')
            ->join('admin_user a', 'e.meter_id=a.admin_id', 'LEFT')
            ->where($where)
            ->whereTime('e.create_time',$time)
            ->count();

        $data['data'] = $list;
        $data['total'] = $count;
        return $data;
    }

    /**
     * @param $id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail($id) {
        $where['id'] = $id;
        $data = Db::name($this->table)
            ->where($where)
            ->find();
        return $data;
    }

    /**
     * 获得未被统计的抄表记录
     */
    public function getNotCount($admin_id) {
        $where['is_statistics'] = 2;
        $where['admin_id'] = $admin_id;
        $data = Db::name($this->table)
            ->where($where)
            ->where('status', '<>', 1)
            ->select();
        return $data;
    }

    /**
     * 获得统计过的最新记录
     * @param $house_id
     * @param $type
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getLastRecord($house_id, $type)
    {
        $where['is_statistics'] = 1;
        $where['house_id'] = $house_id;
        $where['type'] = $type;
        $data = Db::name($this->table)
            ->where($where)
            ->order('create_time desc')
            ->find();
        return $data;
    }

    /**
     * 根据房源id和类型获得未被统计的抄表记录
     */
    public function getNotCountHouse($house_id, $type) {
        $where['is_statistics'] = 2;
        $where['status'] = 2;
        $where['house_id'] = $house_id;
        $where['type'] = $type;
        $data = Db::name($this->table)
            ->where($where)
            ->select();
        return $data;
    }

    /**
     * @param $tung_num
     * @param $unit_num
     * @param $layer_num
     * @param $room_number
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function ListRoomNum($tung_num, $unit_num, $layer_num, $room_number) {
        $where['tung_num'] = $tung_num;
        $where['unit_num'] = $unit_num;
        $where['layer_num'] = $layer_num;
        $where['room_number'] = $room_number;
        $where['is_statistics'] = 2;
        $data = Db::name($this->table)
            ->where($where)
            ->select();
        return $data;
    }

}